#!/bin/sh -e

if [ "x${COVERITY_SCAN_PROJECT_NAME}" != "x" ] ; then exit 0; fi

. CI/travis/lib.sh

INSIDE_DOCKER_TRAVIS_CI_ENV="$INSIDE_DOCKER_TRAVIS_CI_ENV CHECK_AGAINST_KERNEL_HEADER CI_BUILD_SPHINX_DOCS"
CPACK_HELP="-DCPACK_SYSTEM_NAME=${ARTIFACTNAME}"

handle_default() {
	echo "### making build dir"
	mkdir -p build
	cd build

	if command_exists $PYTHON ; then
		command -v $PYTHON
		$PYTHON --version

		PYTHON_HELP="-DPYTHON_BINDINGS=ON \
			-DPYTHON_INCLUDE_DIR=$($PYTHON -c "from distutils.sysconfig import get_python_inc; print(get_python_inc())") \
			-DPYTHON_LIBRARY=$($PYTHON -c "import distutils.sysconfig as sysconfig; print(sysconfig.get_config_var('LIBDIR'))") \
			-DPYTHON_EXECUTABLE=$(command -v $PYTHON)"

		# Add ~/.local dir to paths; this is where pip installs things (if no sudo)
		export PATH="$PATH:$(readlink -f ~/.local/bin/)"
		export PYTHONPATH="$(readlink -f ~/.local/lib/python*/site-packages/)"

		if command_exists sphinx-build ; then
			DOC_HELP="-DWITH_DOC=ON"
		fi
	else
		echo No Python
		if command_exists doxygen ; then
			DOC_HELP="-DWITH_DOC=ON"
		fi
	fi
	if [ ! -n "${DOC_HELP}" ] ; then
		if command_exists man2html ; then
			echo doc and man
			MAN_HELP="-DWITH_MAN=ON"
		fi
	else
		MAN_HELP="-DWITH_MAN=ON"
	fi

	FLAGS="-DENABLE_PACKAGING=ON -DDEB_DETECT_DEPENDENCIES=ON -DWITH_EXAMPLES=ON ${PYTHON_HELP} ${DOC_HELP} ${MAN_HELP} ${CPACK_HELP}"

	# Ubuntu Xenial's libserialport and libzstd are too old
	if [ "${OS_VERSION}" = xenial ] ; then
		FLAGS="${FLAGS} -DWITH_SERIAL_BACKEND=OFF"
	else
		FLAGS="${FLAGS} -DWITH_SERIAL_BACKEND=ON -DWITH_ZSTD=ON"
	fi

	echo "### cmake ${FLAGS}"
	cmake ${FLAGS} ..

	if [ -f CMakeFiles/CMakeError.log ] ; then
		echo "### CMakeError.log"
		cat CMakeFiles/CMakeError.log
		exit 1
	fi

	echo "### make"
	make

	# check the error output if either file is not empty
	if [ -s ./Dox_output_libiio -o -s ./Dox_output_csharp -o -s ./Spx_output_python ] ; then
		if [ -s ./Dox_output_libiio ] ; then
			echo "### ERRORs in Dox_output_libiio"
			cat ./Dox_output_libiio
		fi
		if [ -s ./Dox_output_csharp ] ; then
			echo "### ERRORs in Dox_output_csharp"
			cat ./Dox_output_csharp
		fi
		if [ -s ./Spx_output_python ] ; then
			echo "### ERRRORs in Spx_output_python"
			cat ./Spx_output_python
		fi
		exit 1
	else
		echo "### No errors in Doc"
		if [ -f Dox_output_csharp ] ; then
			ls -l Dox_output_csharp
		fi
		if [ -f Dox_output_libiio ] ; then
			ls -l Dox_output_libiio
		fi
		if [ -f Spx_output_python ] ; then
			ls -l Spx_output_python
		fi
	fi

	echo "### make package"
	make package
	
	if [ -f "./generateDocumentationAndDeploy.sh" ] ; then
		sh generateDocumentationAndDeploy.sh
	fi
	cd ..

	# make sure we are up to date (once)
	if [ "$CHECK_AGAINST_KERNEL_HEADER" = "1" ] ; then
		./CI/travis/check_kernel.sh
	fi
	echo "### All done building"
}

handle_centos() {
	echo "handle centos"
	mkdir -p build
	cd build

	FLAGS="-DENABLE_PACKAGING=ON -DPYTHON_BINDINGS=ON -DWITH_SERIAL_BACKEND=OFF -DWITH_ZSTD=ON ${CPACK_HELP}"

	# CentOS 7's kernel headers are too old for USB support in IIOD
	[ "${OS_VERSION}" = centos7 ] && FLAGS="${FLAGS} -DWITH_IIOD_USBD=OFF"

	cmake ${FLAGS} ..
	make
	make package
	cd ..
}

handle_ubuntu() {
	handle_default
}

handle_debian() {
	handle_default
}

handle_generic_docker() {
	run_docker_script inside_docker.sh
}

handle_doxygen() {
	handle_default
}

setup_build_type_env_vars

handle_${BUILD_TYPE}
